<?php

namespace App\Http\Controllers\Admin;

use App\Models\BucketCategory;
use App\Models\Shop;
use App\Models\ShopWaterBillCategory;
use App\Models\ShopWaterCategory;
use App\Models\WaterBillCategory;


/**
 * 商品分类管理控制器
 */
class ShopWaterBillCategoryController extends BaseController
{

    public function __construct(ShopWaterBillCategory $shopWaterBillCategory, Shop $shop, WaterBillCategory $waterBillCategory)
    {
        parent::__construct();
        $this->waterBillCategory = $waterBillCategory;
        $this->shopWaterBillCategory = $shopWaterBillCategory;
        $this->shop = $shop;
    }

    public function getIndex()
    {
        $shop = $this->shop->_getOneById(request('shop_id'));
        $cate = $this->shopWaterBillCategory->_getOne(['shop_id' => request('shop_id')]);
        if ($cate) {
            $cate = $cate->toArray();
        }
        return view('admin.shop_waterbill_category.index', compact('cate', 'shop'));
    }

    public function getStore()
    {
        if (!request('shop_id')) {
            return $this->error('非法操作');
        }
        $info = $this->shopWaterBillCategory->_getOne(['shop_id' => request('shop_id')]);
        if ($info) {
            $info = $info->toArray();
            $info['ids'] = [];
            foreach ($info['category_data'] as $k => $v) {
                $info['ids'][] = $v['id'];
            }
        }
        $rows = $this->waterBillCategory->_getList(['admin_id' => is_login(), 'status' => 1], '*', ['sort' => 'asc'])->toArray();
        return view('admin.shop_waterbill_category.store', compact('rows', 'info'));
    }

    public function postStore()
    {
        $res = $this->shopWaterBillCategory->addOrUpdate();
        if (!$res) {
            return $this->error($this->shopWaterBillCategory->getError() ? $this->shopWaterBillCategory->getError() : '未知错误');
        }
        return $this->success('操作成功', urlAdmin('shopWaterBillCategory', 'getIndex', ['shop_id' => request('shop_id')]));
    }


    /* 新增分类 */
    public function getAdd()
    {
        return view('admin.shop_waterbill_category.edit', compact('category'));
    }

    public function postAdd()
    {
        $res = $this->waterBillCategory->addOrUpdate();
        if (!$res) {
            return $this->error($this->waterBillCategory->getError() ? $this->waterBillCategory->getError() : '新增失败');
        }
        return $this->success('新增成功', urlAdmin('shopWaterBillCategory', 'getIndex', ['shop_id' => request('shop_id')]));
    }

    public function anyChangeStatus()
    {
        $method = request('method');
        $ids = array_unique((array)request('ids'));
        if (empty($ids)) {
            return $this->error('请选择要操作的数据!');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        if (!request('shop_id')) {
            return $this->error('非法操作');
        }
        $info = $this->shopWaterBillCategory->_getOne(['shop_id' => request('shop_id')])->toArray();
        if (empty($info['category_data'])) {
            return $this->error('非法操作!');
        }
        foreach ($ids as $k => $v) {
            foreach ($info['category_data'] as $m => $n) {
                if ($v == $n['id']) {
                    $info['category_data'][$m]['status'] = intval(request('status'));
                }
            }
        }
       $res = $this->shopWaterBillCategory->_save($info);
        if (!$res) {
            return $this->error('操作失败');
        }
        return $this->success('操作成功');
    }

}

